package com.zsr.example.controller.front;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsr.example.entity.EduCourse;
import com.zsr.example.service.EduCourseService;
import com.zsr.example.service.EduSubjectService;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.vo.front.CourseFrontReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zsr
 * @date 2021/6/20 21:32
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseController {
    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduSubjectService subjectService;


    /**
     * 条件查询分页查询课程
     *
     * @param page
     * @param limit
     * @param courseFrontVo
     * @return
     */
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public JSONObject getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                         @RequestBody(required = false) CourseFrontReq courseFrontVo) {
        Page<EduCourse> pageCourse = new Page<>(page, limit);
        Map<String, Object> map = courseService.getCourseFrontList(pageCourse, courseFrontVo);
        // 返回分页所有数据
        return CommonUtil.successJson(map);
    }

    /**
     * 根据课程id获取信息
     *
     * @param courseId
     * @param request
     * @return
     */
    @GetMapping(value = "{courseId}")
    public JSONObject getById(
            @PathVariable String courseId
            , HttpServletRequest request) {

        Map<String, Object> result = courseService.getCourseFrontById(courseId);

        return CommonUtil.successJson(result);
    }

    /**
     * 课程分类列表[树形]
     *
     * @return
     */
    @GetMapping("getSubject")
    public JSONObject getSubject() {
        return CommonUtil.successJson(subjectService.getAllSubject());
    }

    /**
     * 根据视频id获取课程信息
     *
     * @param vid 视频id
     * @return
     */
    @GetMapping("getSectionInfo/{vid}")
    public JSONObject getCourseBySectionID(@PathVariable String vid) {

        return CommonUtil.successJson(courseService.getCourseFrontBySectionID(vid));
    }


}

